package com.itheima.mall.web.pms;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.commons.result.Result;
import com.itheima.mall.dubbo.api.pms.PmsProductService;
import com.itheima.mall.entity.domain.pms.Product;
import com.itheima.mall.entity.page.MyPage;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
@CrossOrigin(origins = "*")
public class PmsProductController {

    @DubboReference
    private PmsProductService pmsProductService;

    //查询商品
    @GetMapping("list")
    public Result list(Integer pageNum, Integer pageSize, String keyword,
                       Integer brandId, Integer productCategoryId, String productSn,
                       Integer publishStatus, Integer verifyStatus) {
        MyPage<Product> productPage = pmsProductService.getPage(pageNum,pageSize,keyword,brandId,productCategoryId,
                productSn,publishStatus,verifyStatus);
        return Result.success(productPage);
    }

    //批量修改删除状态
    @PostMapping("/update/deleteStatus")
    public Result deleteStatus(@RequestParam List<Long> ids, @RequestParam Integer deleteStatus) {
        List<Product> listByIds = pmsProductService.listByIds(ids);
        for (Product listById : listByIds) {
            listById.setDeleteStatus(deleteStatus);
            pmsProductService.updateById(listById);
        }
        return Result.success("删除成功");
    }

    //批量上下架商品
    @PostMapping("/update/publishStatus")
    public Result publishStatus(@RequestParam List<Long> ids, @RequestParam Integer publishStatus) {
        List<Product> listByIds = pmsProductService.listByIds(ids);
        for (Product listById : listByIds) {
            listById.setPublishStatus(publishStatus);
            pmsProductService.updateById(listById);
        }
        return Result.success("修改成功");
    }

    //批量设为新品
    @PostMapping("/update/newStatus")
    public Result newStatus(@RequestParam List<Long> ids, @RequestParam Integer newStatus) {
        List<Product> listByIds = pmsProductService.listByIds(ids);
        for (Product listById : listByIds) {
            listById.setNewStatus(newStatus);
            pmsProductService.updateById(listById);
        }
        return Result.success("修改成功");
    }

    //批量推荐商品
    @PostMapping("/update/recommendStatus")
    public Result recommendStatus(@RequestParam List<Long> ids, @RequestParam Integer recommendStatus) {
        List<Product> listByIds = pmsProductService.listByIds(ids);
        for (Product listById : listByIds) {
            listById.setRecommandStatus(recommendStatus);
            pmsProductService.updateById(listById);
        }
        return Result.success("修改成功");
    }

    //批量修改审核状态
    @PostMapping("/update/verifyStatus")
    public Result verifyStatus(@RequestParam String detail, @RequestParam List<Long> ids, @RequestParam Integer verifyStatus) {
        List<Product> listByIds = pmsProductService.listByIds(ids);
        for (Product listById : listByIds) {
            listById.setDetailDesc(detail);
            listById.setVerifyStatus(verifyStatus);
            pmsProductService.updateById(listById);
        }
        return Result.success("修改成功");
    }

    //根据商品名称或货号模糊查询
    @GetMapping("simpleList")
    public Result simpleList(@RequestParam String keyword){
        List<Product> product = pmsProductService.getKeyword(keyword);
       return Result.success(product);
    }
    //更新商品
    @PostMapping("/update/{id}")
    public Result update(@RequestParam Long id,@RequestBody Product product){
        product.setId(id);
        pmsProductService.updateById(product);
        return Result.success("修改成功");
    }
    //根据商品id获取商品编辑信息
    @GetMapping("/updateInfo/{id}")
    public Result updateInfo(Long id){
        Product product = pmsProductService.getById(id);
        return Result.success(product);
    }
    //创建商品
    @PostMapping("/create")
    public Result create(@RequestBody Product product){
        pmsProductService.save(product);
        return Result.success("添加成功");
    }
}
